package com.songlanyun.modules.studyingDetail.controller.api;


import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.utils.StatusMsgEnum;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.studying.entity.Studying;
import com.songlanyun.modules.studying.service.StudyingService;
import com.songlanyun.modules.studyingDetail.entity.StudyingDetail;
import com.songlanyun.modules.studyingDetail.service.StudyingDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/studyingDetail")
public class ApiStudyingDetailController extends AbstractApiController {


    @Autowired
    private StudyingDetailService studyingDetailService;
    @Autowired
    private StudyingService studyingService;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        if (ObjectUtil.isNull(params.get("studying"))) {
            throw new RRException("所属课程不能为空");
        }
        studyingService.loadById(Long.valueOf(params.get("studying").toString()), true);
        params.put("apiUserId", getUserId(true));
        params.put("has_status", 1);

        PageUtils page = studyingDetailService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 播放
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id) {
        StudyingDetail studyingDetail = studyingDetailService.loadById(id, true);
        //查询课程
        Studying studying = studyingService.loadById(studyingDetail.getStudying(), true);
//        if (studying.getHasVip() && GoodsConstant.ServicePackageArea.NOT == getMember().getServiceSystemProvince()) {
//            return R.error(StatusMsgEnum.MEMBER_INSUFFICIENT_PRIVILEGES);
//        }
        //点击量加1
        studyingDetailService.addVideoClick(id, 1);
        return R.ok().put("data", studyingDetail);
    }

}
